Skip to content

fix: Skip WHERE when computed search conditions are empty #544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 17, 2025

Conversation

madalinoprea
Copy link
Contributor

@madalinoprea madalinoprea commented Feb 2, 2022

Given an Active Record model with PK of type UUID:

+class CreateCardTransactions < ActiveRecord::Migration[6.0]
+  def change
+    create_table :card_transactions, id: :uuid do |t|
+      t.timestamps
+    end
+  end
+end

And with a Forest collection with search_fields configured only for id
field:


module Forest
  class CardTransaction
    include ForestLiana::Collection

    collection :Card__Transaction

    search_fields %w[id]
  end
end

SearchQueryBuilder will generate an invalid SQL query with an empty
WHERE statement if search term doesn't meet REGEX_UUID format:

SELECT
    "transactions"."id" AS t0_r0,
    "transactions"."processed_at" AS
FROM "transactions"
WHERE  ORDER BY "transactions"."created_at"
DESC LIMIT $1 OFFSET $2  [["LIMIT", 10], ["OFFSET", 0]]

Definition of Done

General

  • Write an explicit title for the Pull Request, following Conventional Commits specification
  • Test manually the implemented changes
  • Validate the code quality (indentation, syntax, style, simplicity, readability)

Security

  • Consider the security impact of the changes made

Given an Active Record model with PK of type UUID:

```
+class CreateCardTransactions < ActiveRecord::Migration[6.0]
+  def change
+    create_table :card_transactions, id: :uuid do |t|
+      t.timestamps
+    end
+  end
+end
```

And with a Forest collection with `search_fields` configured only for `id`
field:

```

module Forest
  class CardTransaction
    include ForestLiana::Collection

    collection :Card__Transaction

    search_fields %w[id]
  end
end
```

`SearchQueryBuilder` will generate an invalid SQL query with an empty
`WHERE` statement if search term doesn't meet `REGEX_UUID` format:

```sql
SELECT
    "transactions"."id" AS t0_r0,
    "transactions"."processed_at" AS
FROM "transactions"
WHERE  ORDER BY "transactions"."created_at"
DESC LIMIT $1 OFFSET $2  [["LIMIT", 10], ["OFFSET", 0]]

```
@madalinoprea madalinoprea changed the title Fix invalid WHERE statement when search conditions are empty fix: Skip WHERE when computed search conditions are empty Feb 2, 2022
@matthv matthv merged commit 5a9aa09 into ForestAdmin:main Jul 17, 2025
forest-bot added a commit that referenced this pull request Jul 17, 2025
## [9.14.7](v9.14.6...v9.14.7) (2025-07-17)

### Bug Fixes

* skip where when computed search conditions are empty ([#544](#544)) ([5a9aa09](5a9aa09))
@forest-bot
Copy link
Member

🎉 This PR is included in version 9.14.7 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants